package com.thz.houserental.domain.requirement;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thz.houserental.domain.house.House;
import com.thz.houserental.infrastructure.auth.AuthUtil;
import com.thz.houserental.infrastructure.table.User;
import com.thz.houserental.infrastructure.web.service.UserService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service
public class RequirementServiceImpl 
        extends ServiceImpl<RequirementMapper, Requirement> 
        implements RequirementService {
    
    private final UserService userService;

    public RequirementServiceImpl(@Lazy UserService userService) {
        this.userService = userService;
    }

    @Override
    public RequirementVo addRequirement(RequirementDto requirement) {
        String currentUserId = AuthUtil.getCurrentUserId();
        Requirement requirementEntity = new Requirement();
        BeanUtil.copyProperties(requirement, requirementEntity);
        
        requirementEntity.setUserId(currentUserId);
        this.save(requirementEntity);

        RequirementVo requirementVo = new RequirementVo();
        BeanUtil.copyProperties(requirementEntity, requirementVo);
        User user = userService.getById(currentUserId);
        requirementVo.setPhone(user.getPhone());
        
        return requirementVo;
    }

    @Override
    public Page<RequirementVo> queryPage(RequirementVo requirementVo, Page<Requirement> requirementPage) {
        return this.baseMapper.queryPage(requirementVo, requirementPage);
    }

}
